package openjdk.jdk8.demo.chap5;

import openjdk.jdk8.demo.chap4.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;
import static openjdk.jdk8.demo.chap4.Dish.menu;


public class Filtering{

    public static void main(String...args){

        // Filtering with predicate
        List<Dish> vegetarianMenu =
            menu.stream()
                .filter(Dish::isVegetarian)
                .collect(toList());

        vegetarianMenu.forEach(System.out::println);

        // Filtering unique elements
        List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
        numbers.stream()
               .filter(i -> i % 2 == 0)
               .distinct()
               .forEach(System.out::println);

        // Truncating a stream
        List<Dish> dishesLimit3 =
            menu.stream()
                .filter(d -> d.getCalories() > 300)
                .limit(3)
                .collect(toList());

        dishesLimit3.forEach(System.out::println);

        System.out.println("*************************");
        // Skipping elements
        List<Dish> dishesSkip2 =
            menu.stream()
                .filter(d -> d.getCalories() > 300)
                .skip(1)
                .collect(toList());

        dishesSkip2.forEach(System.out::println);


        System.out.println(">>>>>>>>>>>>>>>>>>");
        final List<String> mu = Arrays.asList(
                        "pork",
                        "beef",
                        "pizza",
                        "prawns",
                        "salmon");
        // filter a collection (num2) via another set  number1.

        List<Dish> rst = mu.stream()
                .flatMap(o->menu.stream().filter(x-> o.equals(x.getName())))
                .collect(Collectors.toList());

        rst.forEach(System.out::println);

    }
}
